﻿/*+===================================================================
File:      EnemyBulletBig.cpp

Summary:   Định nghĩa các phương thức của CEnemyBulletBig.
===================================================================+*/

#include "BossThreeBullet.h"
#include "Explosion.h"
#include "PlayState.h"


CBossThreeBullet::CBossThreeBullet(D3DXVECTOR2 pos, int id, int typeID, float angle) :
CBullet(pos, id, typeID, angle)
{
	m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_F_BULLET);
	m_v.y = -350.0f;

	m_box.width = 16.0f;
	m_box.height = 16.0f;
}


CBossThreeBullet::~CBossThreeBullet()
{

}

void CBossThreeBullet::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	D3DXVECTOR2 posInViewPort = CViewport::GetInstance()->TransformMatrix(m_pos);

	if (posInViewPort.x < 0 || posInViewPort.x > SCREEN_WIDTH || posInViewPort.y < 0 || posInViewPort.y > SCREEN_HEIGHT)
	{
		m_bIsLive = false;
		m_pContainerGun->ReloadBullet();
		return;
	}
	
	m_pSprite->Update(timeFrame);
	UpdateBox(timeFrame);
}

void CBossThreeBullet::UpdateCollision(vector<CGameObject*> vecGameObjs, float timeFrame)
{
	if (!m_bIsLive)
		return;

	float timeCollision = timeFrame;
	float normalx = 0.0f, normaly = 0.0f;
	CGameObject* objCollision = NULL;

	//Va chạm với đạn của bill
	for (int i = 0; i < CPlayState::s_billBullets.size(); i++)
	{
		float t = CheckCollision(CPlayState::s_billBullets[i], normalx, normaly, timeFrame);
		if (t < timeFrame && t < timeCollision)
		{
			objCollision = CPlayState::s_billBullets[i];
			timeCollision = t;
		}
	}

	// Xử lí va chạm
	if (objCollision != NULL)
	{
		m_v += m_a * timeCollision;
		m_pos += m_v * timeCollision;
		m_v.x = 0.0f;
		m_v.y = 0.0f;
		objCollision->Kill();
		this->Kill();
	}

	timeCollision = timeFrame;
	normalx = 0.0f, normaly = 0.0f;
	objCollision = NULL;

	//Va chạm với đất
	for (int i = 0; i < vecGameObjs.size(); i++)
	{
		float t = CheckCollision(vecGameObjs[i], normalx, normaly, timeFrame);
		if (t < timeFrame && t < timeCollision)
		{
			switch (vecGameObjs[i]->GetTypeId())
			{
			case ID_GRASS_CAN_FALL:
			case ID_GRASS:
				if (normaly == 1.0f)
				{
					objCollision = vecGameObjs[i];
					timeCollision = t;
				}
				break;
			}
		}
	}

	// Xử lí va chạm
	if (objCollision != NULL)
	{
		m_v += m_a * timeCollision;
		m_pos += m_v * timeCollision;
		m_v.x = 0.0f;
		m_v.y = 0.0f;

		this->Kill();
	}

	m_pos += m_v * timeFrame;
	m_v += m_a* timeFrame;
}

bool CBossThreeBullet::Kill()
{
	m_bIsLive = false;
	m_pContainerGun->ReloadBullet();

	CExplosion* ex = new CExplosion(m_pos, 0, ID_BIG_EXPLOSION, true);
	CPlayState::s_objsOutSideQuadTree.push_back(ex);

	return true;
}